﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Data;

public partial class Add_user : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["Dangnhap"]) == "")
            Response.Redirect("~/Admin/Login.aspx");

        else
            Response.Write("Xin chào " + Convert.ToString(Session["Dangnhap"]));
    }
    protected void bntAdd_Click(object sender, EventArgs e)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] hashedBytes;
        UTF8Encoding encoder = new UTF8Encoding();
        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(txtPassword.Text));
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string strInsert = "INSERT INTO tblUser(userName,password,fullname,email,roleId)VALUES";
        strInsert += "(@userN,@passW,@fullN,@mail,@roleID)";
        SqlCommand sqlCmd = new SqlCommand(strInsert, conn);
        sqlCmd.Parameters.AddWithValue("@userN", txtUsername.Text);
        sqlCmd.Parameters.AddWithValue("@passW", encoder.GetString(hashedBytes));
        sqlCmd.Parameters.AddWithValue("@fullN", txtFullname.Text);
        sqlCmd.Parameters.AddWithValue("@mail", txtEmail.Text);
        sqlCmd.Parameters.AddWithValue("@roleID",ddlRoleid.SelectedIndex);
        conn.Open();
        sqlCmd.ExecuteNonQuery();
        conn.Close();
        txtUsername.Text = "";
        txtPassword.Text = "";
        txtFullname.Text = "";
        lblMsg.Text =  " Created user succecfully ";

    }

}
